package com.ryskoo.framework.core.utils;


import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateFormatHelper {
    public static final String FORMAT_TIME_STR = "yyyy-MM-dd HH:mm:ss";
    public static final String FORMAT_DATE_STR = "yyyy-MM-dd";
    public static final String FORMAT_DATE_STR2 = "yyyyMMdd";
    public static final String FORMAT_DATE_STR3 = "HH:mm:ss";

    private DateFormatHelper() {
    }

    public static String getTimeStr(String formatStr, Date d) {
        DateFormat df = new SimpleDateFormat(formatStr);

        return df.format(d);
    }

    public static String getTimeStr(Date d) {
        return getTimeStr("yyyy-MM-dd HH:mm:ss", d);
    }

    public static String getNowTimeStr(String formatStr) {
        DateFormat df = new SimpleDateFormat(formatStr);

        return df.format(new Date());
    }

    public static String getNowTimeStr() {
        return getNowTimeStr("yyyy-MM-dd HH:mm:ss");
    }

    public static Date getDateBefore(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(5, now.get(5) - day);
        return now.getTime();
    }

    /**
     * @param
     * @return Date
     * Description: 前几分钟
     * @author null
     * @date:2017-3-16 下午1:48:36
     * @version 1.0
     */
    public static Date getTimeBefore(Date d, int minute) {
        Calendar now = Calendar.getInstance();

        now.setTime(d);
        now.add(Calendar.MINUTE, -minute);
        return now.getTime();
    }

    /**
     * @param
     * @return Date
     * Description:后几分钟
     * @author null
     * @date:2017-3-21 下午1:05:51
     * @version 1.0
     */
    public static Date getTimeMinuteAfter(Date d, int minute) {
        Calendar now = Calendar.getInstance();

        now.setTime(d);
        now.add(Calendar.MINUTE, minute);
        return now.getTime();
    }

    public static Date getDateAfter(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(5, now.get(5) + day);
        return now.getTime();
    }

    public static Date toDate(String sDate) {
        if (StringUtils.isEmpty(sDate)) {
            return null;
        }
        if (sDate.length() <= 10) {
            sDate = sDate + " 00:00:00";
        }

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dt = null;
        try {
            dt = formatter.parse(sDate);
        } catch (Exception e) {
            e.printStackTrace();
            dt = null;
        }
        return dt;
    }

    public static long toTime(String sDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dt = null;
        long time = 0;
        try {
            dt = formatter.parse(sDate);
            time = dt.getTime();
        } catch (Exception e) {
            e.printStackTrace();
            dt = null;
        }
        return time;
    }

    /**
     * @param @param  time
     * @param @param  formatStr 日期格式
     * @param @return 设定文件
     * @throws
     * @Description: 检查日期格式
     * @time 2017-8-18 下午7:53:21
     * @author Y2008405102
     */
    public static boolean isValidDate(String time, String formatStr) {
        boolean convertSuccess = true;
        // 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写；
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        try {
            // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
            format.setLenient(false);
            format.parse(time);
        } catch (ParseException e) {
            // e.printStackTrace();
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            convertSuccess = false;
        }
        return convertSuccess;
    }
    
    
	/**
	 * 判断 2个日期的大小 beginTime<endTime 返回true
	 * 
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	public static boolean compareTime(String beginTime, String endTime, String format) {
		DateFormat df = new SimpleDateFormat(format);
		try {
			Date dt1 = df.parse(beginTime);
			Date dt2 = df.parse(endTime);
			if (dt1.getTime() < dt2.getTime()) {
				return true;
			}
		} catch (ParseException e) {
		}
		return false;
	}

}
